package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.CodeUser;
import cn.com.connext.scrm.domain.master.PrizeGood;
import cn.com.connext.scrm.repository.master.CodeUserRepository;
import cn.com.connext.scrm.web.rest.vm.CodeUserVM;
import cn.com.connext.scrm.web.rest.vm.NewCodeVM;
import cn.com.connext.scrm.web.rest.vm.NewPrizeVM;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CodeUserService {

    private final Logger log = LoggerFactory.getLogger(CodeUserService.class);

    @Autowired
    CodeUserRepository codeUserRepository;

    public void codeEdit(NewPrizeVM vm) throws ParseException {
        CodeUser codeUser =new CodeUser();
        codeUser.setCodeId(vm.getCodeId());
        codeUser.setUnionId(vm.getUnionId());
        DateFormat dateInstance = DateFormat.getDateInstance();

        String format = dateInstance.format(new Date());
        Date createtime = dateInstance.parse(format);
        codeUser.setCreatetime(createtime);
        codeUserRepository.saveAndFlush(codeUser);
    }

    public List<CodeUser> findByUnionIdAndCodeId(String unionId, String codeId) {
        List<CodeUser> byUnionIdAndCodeId = codeUserRepository.findByUnionIdAndCodeId(unionId, codeId);
        return byUnionIdAndCodeId;
    }

    public List<CodeUser> findByCodeId(String codeId) {
        List<CodeUser> byCodeId = codeUserRepository.findByCodeId(codeId);
        return byCodeId;
    }

    public List<CodeUser> findByUnionIdAndCreatetimeLike(String unionId,Date createtime) {
        List<CodeUser> byUnionIdAndCreaetime = codeUserRepository.findByUnionIdAndCreatetimeLike(unionId,createtime);
        return byUnionIdAndCreaetime;
    }


}
